package com.ruoyi.web.controller.iot;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.IotTaskList;
import com.ruoyi.system.service.IIotTaskListService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务列表Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/exclude/taskList")
public class IotTaskListController extends BaseController
{
    @Autowired
    private IIotTaskListService iotTaskListService;

    /**
     * 查询任务列表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IotTaskList iotTaskList)
    {
        startPage();
        List<IotTaskList> list = iotTaskListService.selectIotTaskListList(iotTaskList);
        return getDataTable(list);
    }

    /**
     * 导出任务列表列表
     */
    @Log(title = "任务列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotTaskList iotTaskList)
    {
        List<IotTaskList> list = iotTaskListService.selectIotTaskListList(iotTaskList);
        ExcelUtil<IotTaskList> util = new ExcelUtil<IotTaskList>(IotTaskList.class);
        util.exportExcel(response, list, "任务列表数据");
    }

    /**
     * 获取任务列表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(iotTaskListService.selectIotTaskListById(id));
    }

    /**
     * 新增任务列表
     */
    @Log(title = "任务列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotTaskList iotTaskList)
    {
        return toAjax(iotTaskListService.insertIotTaskList(iotTaskList));
    }

    /**
     * 修改任务列表
     */
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotTaskList iotTaskList)
    {
        return toAjax(iotTaskListService.updateIotTaskList(iotTaskList));
    }

    /**
     * 删除任务列表
     */
    @Log(title = "任务列表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(iotTaskListService.deleteIotTaskListByIds(ids));
    }

    /**
     * 根据设备编号获取任务
     */
    @GetMapping("/getTask/{deviceCode}")
    public AjaxResult getTaskByDeviceCode(@PathVariable("deviceCode") String deviceCode)
    {
        IotTaskList task = iotTaskListService.selectIotTaskListByDeviceCode(deviceCode);
        if (task != null) {
            iotTaskListService.updateTaskStatusByDeviceCode(deviceCode);
        }
        return success(task);
    }

    /**
     * 重刷任务
     */
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PutMapping("/refresh/{taskId}")
    public AjaxResult refresh(@PathVariable("taskId") Integer taskId)
    {
        IotTaskList task = new IotTaskList();
        task.setId(taskId);
        task.setStatus(0); // 设置为待执行状态
        task.setUpdateTime((int)(System.currentTimeMillis() / 1000));
        return toAjax(iotTaskListService.updateTaskStatus(task));
    }

    /**
     * 一键重刷
     */
    @Log(title = "任务列表", businessType = BusinessType.UPDATE)
    @PutMapping("/refreshAll")
    public AjaxResult refreshAll(@RequestBody Integer[] taskIds)
    {
        return toAjax(iotTaskListService.updateTaskStatusByIds(taskIds));
    }
} 